package com.mxchip.livestarmobile.mobile.ui.meeting

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.Appli
import com.mxchip.livestarmobile.mobile.base.getString
import com.mxchip.livestarmobile.mobile.db.AppDatabase
import com.mxchip.livestarmobile.mobile.db.entity.CallLog
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.mxchip.livestarmobile.mobile.state.SystemData
import com.mxchip.livestarmobile.mobile.ui.Call
import com.mxchip.livestarmobile.mobile.ui.GroupCall
import com.mxchip.livestarmobile.mobile.ui.P2PSingleCall
import com.orhanobut.hawk.Hawk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.*

class MeetingViewModel(application: Application) : AndroidViewModel(application) {

    private val userId: String = Hawk.get(AppConstant.SP_LOGIN_USER_ID)

    val meetingId = MutableLiveData<String>()

    val liveCount = MutableLiveData<Int>()

    val inviteIds = MutableLiveData<List<String>>()

    var camera: Boolean = false
    var mic: Boolean = false

    private val appDatabase: AppDatabase = (application as Appli).database

    init {
        meetingId.value = "9005737835"
        liveCount.value = 0
        inviteIds.value = ArrayList()
    }

    fun insertCallLog(number: String) {

        val entity = CallLog(userId = userId)

        entity.type = "2"
        entity.number = number
        entity.startTime = Date()
        entity.isCaller = true

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.calllogDao().insertAll(entity)
        }
    }

    fun makeCall(number:String ): Call {

        if(number.isNullOrBlank()) {
            throw Throwable("号码为空")
        }

        val call = when {
            number.length <= 8 -> {

                val aNumber = number.padStart(8, '0')

                if(aNumber == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(aNumber)
            }

            number.length == 11 -> {

                if(number == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(number)
            }

            number.length == 12 -> {
                if (number.contains("#")){
                    P2PSingleCall(number)
                }else{
                    GroupCall(number)
                }
            }

            number.length == 15 -> {

                if (number.contains("#")) {
                    P2PSingleCall(number)
                } else {
                    throw Throwable(getString(R.string.called_number_error))
                }
            }

            number.length == 16 -> {
                if (number.contains("#")){
                    GroupCall(number)
                }else{
                    throw Throwable(getString(R.string.called_number_error))
                }
            }
            else -> {
                throw Throwable(getString(R.string.called_number_error))
            }
        }

        return call
    }
}